<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      * {
        margin: 0;
        padding: 0;
        background-color: #333;
      }
    </style>
  </head>
  <body>
    <button>按钮</button>
    <input type="text" />
    <button class="cancle">取消</button>

    <script>
      // 事件按照一定的时间间隔触发
      const btnEl = document.querySelector('button');
      const inputEl = document.querySelector('input');
      const cancleBtnEl = document.querySelector('.cancle');

      // 节流函数(简单实现版)
      function myThrottle(callback, delay) {
        let startTime = 0;
        const execution = function (...args) {
          const currentTime = Date.now();
          const waitTime = delay - (currentTime - startTime);
          if (waitTime <= 0) {
            callback.apply(this, args);
            startTime = currentTime;
          }
        };
        return execution;
      }

      // 输入事件
      inputEl.addEventListener(
        'input',
        myThrottle(function (event) {
          console.log(this, event);
        }, 1500)
      );

      // 取消事件
      cancleBtnEl.addEventListener('click', () => {
        debounce.cancel();
      });
    </script>
  </body>
</html>
